package com.zy.servlet.yhgl;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zhang.sys.SysGroup;
import com.zhang.sys.SysGroupDAO;
import com.zhang.sys.SysUser;
import com.zy.service.GroupService;

public class GetComboTree extends HttpServlet {

	private static final Logger log = LoggerFactory
			.getLogger(GetGroupMenu.class);

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("appliaction/x-json;charset=utf-8");
		PrintWriter out = response.getWriter();
		String orgId = request.getParameter("id");

		if (orgId == null || orgId.equals("")) {
			orgId = "0";
		}
		GroupService groupService = new GroupService();
		List<Map<String, Object>> list = groupService.getChildrenNodes();
		List<Map<String, Object>> list1 = new ArrayList<Map<String, Object>>();
		if ("rootNode".equals(orgId)) {
			list1.add(list.get(0));
		} else if ("1".equals(orgId)) {
			SysGroup shcssj = null;
			Map<String, Object> map = null;
			SysGroupDAO dao = new SysGroupDAO();
			List<Map<String, Object>> ShcssList = dao.findAll();
			for (int i = 1; ShcssList != null && i < ShcssList.size(); i++) {
				shcssj = (SysGroup) ShcssList.get(i);
				map = new HashMap<String, Object>();
				map.put("id", shcssj.getGroupId());
				map.put("text", shcssj.getGroupName());
				map.put("iconCls", "icon-group");
				map.put("expanded", false);
				map.put("leaf", true);
				list1.add(map);
				map = null;
			}
			dao.getSession().close();
		}

		JSONArray obj = JSONArray.fromObject(list1);
		out.println(obj.toString());
		out.flush();
		out.close();
	}

}
